package com.oliet.generate;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.google.common.collect.Lists;
import com.oliet.entity.base.BaseEntity;

import java.util.Collections;
import java.util.List;

/**
 * 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
 */
public class CodeGenerator {


    public static void main(String[] args) {
        // 数据源配置
        String jdbcUrl = "jdbc:mysql://localhost:3306/oliet_management_platform?useSSL=false";
        String jdbcUsername = "root";
        String jdbcPassword = "root";
        List<String> tableNameList = Lists.newArrayList();
        tableNameList.add("dictionary");




        System.out.println(System.getProperty("user.dir") + "\\src\\main\\java\\");

        FastAutoGenerator.create(jdbcUrl, jdbcUsername, jdbcPassword)
                .globalConfig(builder -> {
                    builder.author("JZP") // 设置作者
//                              .enableSwagger() // 开启 swagger 模式
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd")
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir") + "\\src\\main\\java\\"); // 指定输出目录
//                            .outputDir("D:\\DeveloperInstall\\IDEA\\private_workspase\\oliet-management-platform\\src\\main\\java\\");
                })
                .packageConfig(builder -> {
                    builder.parent("net") // 设置父包名
                            .moduleName("test") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml
                                    , System.getProperty("user.dir") + "\\src\\main\\resources\\mapper\\mysql\\")); // 设置mapperXml生成路径
//                                    , "D:\\DeveloperInstall\\IDEA\\private_workspase\\oliet-management-platform\\src\\main\\resources\\mappers\\mysql\\"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableNameList)
                            .enableCapitalMode()
                            .enableSkipView()
                            .disableSqlFilter()
                            .entityBuilder()
                            .superClass(BaseEntity.class)//实体父类
                            .enableLombok()
                            .versionPropertyName("version")
                            .logicDeleteColumnName("is_deleted")
                            .logicDeletePropertyName("is_deleted")
                            .naming(NamingStrategy.no_change)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addSuperEntityColumns("id","version","code"
                                    ,"date_created","created_by_code","created_by_name"
                                    ,"last_updated","modified_by_code","modified_by_name"
                                    ,"is_delete")//实体父类中的公共属性
                            .addTableFills(new Column("version", FieldFill.INSERT))
                            .addTableFills(new Column("date_created", FieldFill.INSERT))
                            .addTableFills(new Column("created_by_code", FieldFill.INSERT))
                            .addTableFills(new Column("created_by_name", FieldFill.INSERT))
                            .addTableFills(new Column("last_updated", FieldFill.INSERT_UPDATE))
                            .addTableFills(new Column("last_updated", FieldFill.INSERT))
                            .addTableFills(new Column("modified_by_code", FieldFill.INSERT_UPDATE))
                            .addTableFills(new Column("modified_by_name", FieldFill.INSERT_UPDATE))
                            .addTableFills(new Column("is_deleted", FieldFill.INSERT))
                            .idType(IdType.AUTO)
                            .naming(NamingStrategy.underline_to_camel)
                            .mapperBuilder()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .enableBaseResultMap()
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .enableHyphenStyle();


                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板 111
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}
